package com.panda.web.service.py.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.panda.web.domain.dto.CompanyDTO;
import com.panda.web.domain.vo.CompanyVO;
import com.panda.web.domain.vo.commons.CompanyJobVO;
import com.panda.web.entity.py.Company;
import com.panda.web.mapper.py.CompanyMapper;
import com.panda.web.service.py.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.ArrayList;

import lombok.extern.slf4j.Slf4j;
import com.panda.web.utils.pageutils.PageResult;
import org.springframework.transaction.annotation.Transactional;


/**
 * 公司Service业务层处理
 *
 * @author cpanda
 * @date 2025-03-03
 */
@Slf4j
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {
    @Autowired
    CompanyMapper companyMapper;




    /**
     * 分页查询
     *
     * @param dto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResult<CompanyVO> PageList(CompanyDTO dto, Integer pageNum, Integer pageSize) {

        Page<Company> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Company> queryWrapper = new QueryWrapper();//需要处理查询条件
        IPage<Company> companyIPage = companyMapper.selectPage(page, queryWrapper);
        long total = companyIPage.getTotal();
        List<Company> records = companyIPage.getRecords();
        List<CompanyVO> voList = companyVoToList(records);//封装vo数据方法

        return new PageResult<>(voList, total, pageNum, pageSize);
    }

    /**
     * 无分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public List<CompanyVO> choiceList(CompanyDTO dto) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper();//需要处理查询条件
        //构建查询条件
        List<Company> companyList = companyMapper.selectList(queryWrapper);
        List<CompanyVO> voList = companyVoToList(companyList);//封装vo数据方法
        return voList;
    }

    /**
     * 回显数据
     *
     * @param companyId
     * @return
     */
    @Override
    public CompanyVO selectById(Long companyId) {
        CompanyVO vo = new CompanyVO();
        Company company = companyMapper.selectById(companyId);
        BeanUtils.copyProperties(company, vo);
        return vo;
    }


    // 处理VO数据封装
    private List<CompanyVO> companyVoToList(List<Company> companyList) {
        List<CompanyVO> voList = new ArrayList<>();
        if (companyList.size() > 0 && !companyList.isEmpty()) {
            for (Company company : companyList) {
                CompanyVO vo = new CompanyVO();
                BeanUtils.copyProperties(company, vo);
                voList.add(vo);
            }
        }
        return voList;
    }


}